package com.log4jviewer.ui.preferences.filters;

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.junit.Test;

/**
 * 
 * @author <a href="mailto:Daniil.Yaroslavtsev@gmail.com">Daniil Yaroslavtsev</a>
 * 
 */
public class ButtonsStateAndActionsTest extends AbstractFiltersPageTest {

    protected static final SWTWorkbenchBot BOT = getBot();

    @Test
    public void testButtonsInitialState() {
        assertTrue(getAddFilterButton().isEnabled());
        assertFalse(getRemoveFilterButton().isEnabled());
        assertFalse(getAddFilterRuleButton().isEnabled());
        assertFalse(getRemoveFilterRuleButton().isEnabled());
    }

    @Test
    public void testButtonsStateOnFilterSelected() {
        addFilter();
        getFilterTable().select(0);
        assertTrue(getAddFilterButton().isEnabled());
        assertTrue(getRemoveFilterButton().isEnabled());
        assertTrue(getAddFilterRuleButton().isEnabled());
        assertFalse(getRemoveFilterRuleButton().isEnabled());
    }

    @Test
    public void testButtonsStateOnFilterRuleSelected() {
        addFilter();
        getFilterTable().select(0);
        addFilterRule();
        getFilterRuleTable().select(0);
        assertTrue(getAddFilterButton().isEnabled());
        assertTrue(getRemoveFilterButton().isEnabled());
        assertTrue(getAddFilterRuleButton().isEnabled());
        assertTrue(getRemoveFilterRuleButton().isEnabled());
    }

    @Test
    public void testAddAndRemoveButtonsActions() {

        int filtersCount = 3;

        // add filters by pressing "Add" button n times.
        addFilters(filtersCount);

        boolean addFilterBtnWorks = (getFilterTable().rowCount() == filtersCount);
        assertTrue(addFilterBtnWorks);

        // add several filter rules for the first filter by pressing "Add" button n times.
        int filterIndexToAddRules = 0;
        addFilterRules(filterIndexToAddRules, filtersCount);

        boolean addFilterRuleBtnWorks = (getFilterRuleTable().rowCount() == filtersCount);
        assertTrue(addFilterRuleBtnWorks);

        // Remove all filters by pressing "Remove" button n times.
        removeAllFilterRules(filterIndexToAddRules);

        boolean removeFilterRuleBtnWorks = (getFilterRuleTable().rowCount() == 0);
        assertTrue(removeFilterRuleBtnWorks);

        // Remove all filters by pressing "Remove" button n times.
        removeAllFilters();

        boolean removeFilterBtnWorks = (getFilterTable().rowCount() == 0);
        assertTrue(removeFilterBtnWorks);
    }

}
